//
//  HOBindPhoneNumberViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOBindPhoneNumberViewController.h"
#import "HOUtils.h"
#import "HOConstants.h"
#import "Logger.h"
#import "HOPWDSettingsViewController.h"

@interface HOBindPhoneNumberViewController ()<HOEditTableViewCellDelegate>
@end

@implementation HOBindPhoneNumberViewController
@synthesize messageId;
@synthesize isBingPhone;
@synthesize settingsViewController;
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initTableView];
//    baseTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    [self setNavigationItemTitle:NSLocalizedString(@"绑定新手机", @"绑定手机")];
//	[self displayRightButtonItemWithTitle:NSLocalizedString(@"确认绑定", @"确认绑定")];
    [super viewWillAppear:animated];
	[self createBtns];
    
}

- (void)rightButtonItemDidClick
{
//	[self confirmBindPhone:nil];
}
- (void)createBtns
{
	UIView *view = [[[UIView alloc]
					initWithFrame:CGRectMake(0,
											 0,
											 self.view.frame.size.width,
											 100)] autorelease];
//	CGFloat offset = 35;
//	if(IS_IPHONE_4_SCREEN)
//		offset = 5;
//	if(IS_IPHONE_5_SCREEN)
//		offset = 25;
	UIButton* nextBtn = [[UIButton alloc]initWithFrame:
				   CGRectMake(HO_HORIZONAL_PADDING,
							  20,
							  self.view.frame.size.width - 2 * HO_HORIZONAL_PADDING,
							  40)];
	[nextBtn.layer setMasksToBounds:YES];
	
	
	[nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
	[nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
	[nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
	[nextBtn setTintColor:[UIColor whiteColor]];
	[nextBtn setTitle:NSLocalizedString(@"确定绑定新号码", @"确定绑定新号码") forState:UIControlStateNormal];
//	[nextBtn addTarget:self action:@selector(confirmBindPhone:) forControlEvents:UIControlEventTouchUpInside];
	[nextBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_NORMAL forView:nextBtn] forState:UIControlStateNormal];
	[nextBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_TOUCHED forView:nextBtn] forState:UIControlStateSelected];
	[nextBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_TOUCHED forView:nextBtn] forState:UIControlStateHighlighted];
	[nextBtn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_DISABLED forView:nextBtn] forState:UIControlStateDisabled];
	nextBtn.layer.cornerRadius = 3;
	[view addSubview:nextBtn];
	baseTableview.tableFooterView = view;
	[nextBtn release];
	
}
- (void)dealloc
{
    [super dealloc];
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOEditTableViewCell *cell = [super tableView:tableView cellForEditRowAtIndexPath:indexPath];
	cell.accessoryType = UITableViewCellAccessoryNone;
	[cell initViewsWithHeight:HO_CELL_HEIGHT withWidth:tableView.frame.size.width withDelegate:self];
	[cell setTextEditing:YES];
	[cell setContent:@""];
	[cell setIndexPath:indexPath];
	[cell getTextField].delegate = self;
	cell.selectionStyle = UITableViewCellSelectionStyleNone;

	[cell getTextField].indexPath= indexPath;
	[cell getTextField].delegate = self;
	return cell;
}
#pragma mark

- (void)textFieldDidChange:(NSNotification *)notification
{
	
	HOTextField *textField = (HOTextField *)notification.object;
	NSIndexPath *indexPath = ((HOTextField *)textField).indexPath;
	if(indexPath.row == 0)
	{
		self.phoneNumber = textField.text;
	}
	else if(indexPath.row == 1)
	{
		self.verifyCode = textField.text;
	}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if(![textField isKindOfClass:[HOTextField class]])
		return NO;
	HOTextField *hoTextField = (HOTextField *)textField;
	NSIndexPath *indexPath = hoTextField.indexPath;
	if((indexPath.row + 1) >= [self tableView:baseTableview numberOfRowsInSection:indexPath.section])
	{
//		[self confirmBindPhone:nil];
		return NO;
	}
	
	NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:(indexPath.row + 1) inSection:0];
	HOEditTableViewCell *cell = (HOEditTableViewCell *)[self tableView:baseTableview cellForRowAtIndexPath:nextIndexPath];
	if(cell)
	{
		[[cell getTextField] resignFirstResponder];
		[[cell getTextField] performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.1f];
		
		return NO;
	}
	return YES;
}
#pragma mark

- (void)didReceiveResponse:(HOBaseResponseEntity *)response
{
	if(![response.action isEqualToString:kHO_ACTION_MobileBind])
		return;
	
	[super didReceiveResponse:response];
	if([response isRequestDone])
	{
		if([response.action isEqualToString:kHO_ACTION_MobileBind])
		{
			[self popupHintView:NSLocalizedString(@"绑定成功!", @"绑定成功!") inTime:1 needCallback:YES];
		}
	}
	else if(response.baseRequest.status == HO_REQUEST_STATUS_DOING)
	{
		[self hideKeyboard];
	}
	
}

- (void)hintDidHide
{
    self.view.userInteractionEnabled = YES;
	if(isBingPhone)
	{
		[self.navigationController popToViewController:self.settingsViewController animated:YES];
	}
	else
	{
		[self goBack];
	}
}

@end
